/*
 *  @product.title@
 *  Copyright (C) @product.copyright-year@
 *    @product.vendor@
 *    Registration Number: @product.registration-number@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.util.castor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.exolab.castor.mapping.ConfigurableFieldHandler;
import org.exolab.castor.mapping.GeneralizedFieldHandler;
import org.exolab.castor.mapping.ValidityException;



/**
 * A Castor FieldHandler for the Date type.
 *
 * The subclasses may extend this class by defining a constructor
 * that passes the date/time format pattern description
 * to this class's constructor.
 *
 * @author  Akihito Nakamura, AIST
 * @version $Id: CastorDateHandler.java 300 2011-03-02 05:45:46Z nakamura5akihito $
 */
public class CastorDateHandler
    extends GeneralizedFieldHandler
    implements ConfigurableFieldHandler
{

    /**
     * The date/time formatter and parser.
     */
    private SimpleDateFormat  _formatter;


    /**
     * The default date format pattern.
     */
    public static final String PATTERN = "yyyy-MM-dd";



    /**
     * Constructor.
     */
    public CastorDateHandler()
    {
        this( PATTERN );
    }



    /**
     * Constructor.
     *
     * @param   pattern
     *  the date/time format pattern description.
     */
    public CastorDateHandler(
                    final String pattern
                    )
    {

        _formatter = new SimpleDateFormat( pattern );
    }



    /**
     */
    public void setPattern(
                    final String pattern
                    )
    {
        _formatter = new SimpleDateFormat( pattern );
    }



    //**************************************************************
    //  ConfigurableFieldHandler
    //**************************************************************

    @Override
    public void setConfiguration(
                    final Properties config
                    )
    throws ValidityException
    {
        String  pattern = config.getProperty( "pattern" );
        if (pattern == null) {
            throw new ValidityException( "no 'pattern' parameter" );
        }

        try {
            _formatter = new SimpleDateFormat( pattern );
                         //throws IllegalArgumentException
        } catch (Exception ex) {
            throw new ValidityException( "invalid 'pattern' parameter: "
                            + ex.getMessage() );
        }
    }



    //**************************************************************
    //  FieldHandler
    //**************************************************************

    // from Date to String
    @Override
    public Object convertUponGet(
                    final Object value
                    )
    {
        if (value == null) {
            return null;
        }

        return _formatter.format( (Date)value );
    }



    // from String to Date
    @Override
    public Object convertUponSet(
                    final Object value
                    )
    {
        Date  date = null;
        if (value != null) {
            try {
                date = _formatter.parse( (String)value );
            } catch (ParseException p_ex) {
                throw new IllegalArgumentException( p_ex.getMessage() );
            }
        }

        return date;
    }



    @Override
    public Class<Date> getFieldType()
    {
        return Date.class;
    }



    @Override
    public Object newInstance(
                    final Object parent
                    )
        throws IllegalStateException
    {
        return null;
    }

}
//extends GeneralizedFieldHandler
//{
//
//    /**
//     * The date/time formatter and parser.
//     */
//    private SimpleDateFormat  _formatter;
//
//
//    /**
//     * The default date format pattern.
//     */
//    public static final String PATTERN = "yyyy-MM-dd";
//
//
//
//
//    /**
//     * Constructor.
//     */
//    public CastorDateHandler()
//    {
//        this( PATTERN );
//    }
//
//
//
//    /**
//     * Constructor.
//     *
//     * @param   pattern
//     *  the date/time format pattern description.
//     */
//    public CastorDateHandler(
//                    final String pattern
//                    )
//    {
//        _formatter = new SimpleDateFormat( pattern );
//    }
//
//
//
//    /**
//     */
//    public void setPattern(
//                    final String pattern
//                    )
//    {
//        _formatter = new SimpleDateFormat( pattern );
//    }
//
//
//
//    //**************************************************************
//    //  GeneralizedFieldHandler
//    //**************************************************************
//
//    // from Date to String
//    public Object convertUponGet(
//                    final Object value
//                    )
//    {
//        if (value == null) {
//            return null;
//        }
//
//        return _formatter.format( (Date)value );
//    }
//
//
//
//    // from String to Date
//    public Object convertUponSet(
//                    final Object value
//                    )
//    {
//        Date  date = null;
//        if (value != null) {
//            try {
//                date = _formatter.parse( (String)value );
//            } catch (ParseException p_ex) {
//                throw new IllegalArgumentException( p_ex.getMessage() );
//            }
//        }
//
//        return date;
//    }
//
//
//
//    public Class<Date> getFieldType()
//    {
//        return Date.class;
//    }
//
//
//
//    public Object newInstance(
//                    final Object parent
//                    )
//        throws IllegalStateException
//    {
//        return null;
//    }
//
//}
// CastorDateHandler
